#ifndef _TCPSERVER_HPP_
#define _TCPSERVER_HPP_

#include "jhdefs.hpp"
#include "types.hpp"
#include "net/ServerSocket.hpp"
#include "net/TCPServerConnection.hpp"
#include "net/TCPServerDispatcher.hpp"
#include "net/TCPServerParams.hpp"
#include "thread/runnable.hpp"
#include "thread/thread.hpp"
#include "thread/threadpool.hpp"
using namespace JHCPP::thread;

NET_NAMESPACE_BEGIN

class CTCPServer : public CRunnable
{
public:
	CTCPServer(CTCPServerConnectionFactory::Ptr pFactory, UInt16 portNumber = 0, CTCPServerParams::Ptr pParams = 0);
	CTCPServer(CTCPServerConnectionFactory::Ptr pFactory, const CServerSocket& socket, CTCPServerParams::Ptr pParams = 0);
	CTCPServer(CTCPServerConnectionFactory::Ptr pFactory,CThreadPool& threadPool, const CServerSocket& socket, CTCPServerParams::Ptr pParams = 0);
	virtual ~CTCPServer();
	const CTCPServerParams& params() const;

	void start();
	void stop();

	int currentThreads() const;
	int maxThreads() const;
	int totalConnections() const;
	int currentConnections() const;
	int maxConcurrentConnections() const;
	int queuedConnections() const;
	int refusedConnections() const;

	const CServerSocket& socket() const;
	UInt16 port() const;

protected:
	void run();
	static std::string threadName(const CServerSocket& socket);

private:
	CTCPServer();
	CTCPServer(const CTCPServer&);
	CTCPServer& operator = (const CTCPServer&);

	CServerSocket			m_socket;
	CTCPServerDispatcher*	m_pDispatcher;
	CThread					m_thread;
	bool					m_stopped;
};


NET_NAMESPACE_END

#endif
